package shuashuami.hb.com.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.util.TextTool;

/**
 * Created by apple on 17/6/14.
 */

public class ShareCodeAdapter extends BaseAdapter{

    List<String> mStrs;

    Context mContext;
    LayoutInflater mInflater;

    public ShareCodeAdapter(List<String> strs, Context context){
        this.mStrs=strs;
        this.mContext=context;
        this.mInflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mStrs.size();
    }

    @Override
    public Object getItem(int i) {
        return mStrs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View converView, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(converView==null){
            holder=new ViewHolder();

            converView=mInflater.inflate(R.layout.list_item_sharecode,null);
            holder.text_ShareCode= (TextView) converView.findViewById(R.id.txt_shareCode);
            holder.btn_ShareCode= (Button) converView.findViewById(R.id.btn_shareCode);

            converView.setTag(holder);
        }else{
            holder=(ViewHolder)converView.getTag();

        }

        holder.text_ShareCode.setText(mStrs.get(i));

        holder.btn_ShareCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开始复制
                TextTool.copy(mStrs.get(i),mContext);
                mHandler.sendEmptyMessage(0x1);
            }
        });

        return converView;
    }

    class ViewHolder{
        TextView text_ShareCode;
        Button btn_ShareCode;

    }

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what=msg.what;
            if (what == 0x1) {
                Toast.makeText(mContext,"复制成功",Toast.LENGTH_LONG).show();
            }
        }
    };
}
